﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using GMS.Model;
using System.Collections.Generic;
using System.Collections; 
/// <summary>
///ApplicationOperation 的摘要说明
///注意：默认modal数据时不可改变的，不然不保证正确。
/// </summary>
/// 
namespace GMS.Web.AppOperation
{
    public static class ApplicationOperation
    {

        //private static HttpApplication app = new HttpApplication();
        //需要在登入成功后为类设置Application值，引用全局的Application对象。

        /// <summary>
        /// 下面是tab_tabinfo和tab_tabcolumn的application,它们是按照一个个用户（mode）来保存的
        /// </summary>
        private static HttpApplicationState Application = HttpContext.Current.Application;

        public static TabInfo GetTabInfo(string tabcode)
        {
            if (Application[tabcode.ToUpper()] != null)
            {
                return (TabInfo)Application[tabcode.ToUpper()];
            }
            else
            {
                return null;
            }
        }

        public static void SetTabInfo(string tabcode, TabInfo ti)
        {
            if (Application[tabcode.ToUpper()] == null)
            {
                Application.Lock();
                Application.Add(tabcode.ToUpper(), ti);
                Application.UnLock();
                return;
            }
            else
            {
                Application.Lock();
                Application[tabcode] = ti;
                Application.UnLock();
            }
        }

        public static void RemoveTabInfo(string tabcode)
        {
            if (Application[tabcode.ToUpper()] != null)
            {
                Application.Lock();
                Application.Remove(tabcode.ToUpper());
                Application.UnLock();
                return;
            }
        }

        public static List<TabColumns> GetTabColumnsList(string tabcode)
        {
            if (Application[tabcode.ToUpper() + "columns"] != null)
            {
                return (List<TabColumns>)Application[tabcode.ToUpper() + "columns"];
            }
            else
            {
                return null;
            }
        }
        public static void SetTabColumnsList(string tabcode, List<TabColumns> TabColumnsList)
        {
            if (Application[tabcode.ToUpper() + "columns"] == null)
            {
                Application.Lock();
                Application.Add(tabcode.ToUpper() + "columns", TabColumnsList);
                Application.UnLock();
                return;
            }
            else
            {
                Application.Lock();
                Application[tabcode.ToUpper() + "columns"] = TabColumnsList;
                Application.UnLock();
            }
        }

        public static void RemoveTabColumnsList(string tabcode)
        {
            if (Application[tabcode.ToUpper() + "columns"] != null)
            {
                Application.Lock();
                Application.Remove(tabcode.ToUpper() + "columns");
                Application.UnLock();
                return;
            }
        }

        ///下面是角色权限的application，是整个作为dataset存的
        public static void SetRoleModuleRight(DataSet ds)
        {            
            Application.Lock();
            Application["RoleModuleRight"] = ds;
            Application.UnLock();           
        }

        public static DataSet GetRoleModuleRight()
        {
            if (Application["RoleModuleRight"] != null)
            {
                return (DataSet)Application["RoleModuleRight"];
            }
            else
            {
                return null;
            }
        }

        public static DataTable GetUserModuleList(int id) 
        {
            if (Application["UserModuleList_hashtable"] != null)
            {
                Hashtable ht = (Hashtable)Application["UserModuleList_hashtable"];
                if (ht.Contains(id.ToString())) return (DataTable)ht[id.ToString()];
            }
            else 
            {
                return null;
            }
            return null;
        }
        public static void SetUserModuleList(int id, DataTable dt)
        {
            if (dt == null) 
            {
                Application.Remove("UserModuleList_hashtable");
            }
            else
            {

                if (Application["UserModuleList_hashtable"] == null)
                {
                    Hashtable ht = new Hashtable();
                    ht.Add(id.ToString(), dt);
                    Application.Lock();
                    Application.Add("UserModuleList_hashtable", ht);
                    Application.UnLock();
                }
                else
                {
                    Hashtable ht = (Hashtable)Application["UserModuleList_hashtable"];
                    ht.Add(id.ToString(), dt);
                }
            }
        }

    }

}